﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
    private SqlCommand CMD;
    private DataTable TB;
    private DataTable TB2;
    private DataSet DS;
    private SqlConnection CON;
    private SqlDataAdapter DA;
    string connectString = WebConfigurationManager.ConnectionStrings["connect"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        DS = new DataSet();
        TB = new DataTable();
        TB = new DataTable();
        TB2 = new DataTable();
        DS = new DataSet();
        CON = new SqlConnection(connectString);
        if (!IsPostBack)
            BlindProductToGridview();
    }

    protected void BlindProductToGridview()
    {
        try
        {
           
            if (CON.State == ConnectionState.Closed)
                CON.Open();
            CMD = new SqlCommand("select * from Product where ProductCategory = 'Pants'", CON);
            DA = new SqlDataAdapter(CMD);
            DA.Fill(TB);
            GridView1.DataSource = TB;
            GridView1.DataBind();
        }

        catch (System.Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    protected void addnew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddNewProducts.aspx");
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string idtxt = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        int id = Convert.ToInt32(idtxt);
        try
        {
            if (CON.State == ConnectionState.Closed)
                CON.Open();
            CMD = new SqlCommand("UpdateProduct", CON);
            CMD.CommandType = CommandType.StoredProcedure;
            CMD.Parameters.Add("@productid", SqlDbType.Int).Value = id;

            CMD.Parameters.Add("@productname", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            CMD.Parameters.Add("@productcategory", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
           
            CMD.Parameters.Add("@productimage", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            CMD.Parameters.Add("@productprice", SqlDbType.Float).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

            CMD.ExecuteNonQuery();
            GridView1.EditIndex = -1;
            BlindProductToGridview();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            CON.Close();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BlindProductToGridview();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
        try
        {
            if (CON.State == ConnectionState.Closed)
                CON.Open();
            CMD = new SqlCommand("spDeleteProduct", CON);
            CMD.CommandType = CommandType.StoredProcedure;
            CMD.Parameters.Add("@id", SqlDbType.Int).Value = id;
            CMD.ExecuteNonQuery();
            BlindProductToGridview();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            CON.Close();
        }   
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BlindProductToGridview();
    }
}